require('babel-polyfill')
require('babel-register')
const http = require('http')
const httpProxy = require('http-proxy')
// 创建一个Proxy Server对象
const proxy = httpProxy.createProxyServer({})
// 捕获异常1
proxy.on('error', function(err, req, res) {
  res.writeHead(500, {
    'Content-Type': 'text/plain'
  })
  res.end('服务端错误')
})




const server = http.createServer(function(req, res)  {
  const host = req.headers.host
  const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
  console.log(`your ip is ${ip}，host is ${host}`)
  switch (host){
    case 'node.ajax1024.com':
      proxy.web(req, res, { target: 'http://127.0.0.1:3000' })
      break;
    default:
      res.writeHead(200, {
        "Content-Type": 'text/plain'
      })
      res.end('welcome to my server')
  }
})
console.log('listening to port 80')
server.listen(80)